{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-16T07:29:18.018139800Z",
     "start_time": "2024-07-16T07:29:17.743119500Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "       Brand  Store Number        Store Name Ownership Type  \\\n0  Starbucks  47370-257954     Meritxell, 96       Licensed   \n1  Starbucks  22331-212325  Ajman Drive Thru       Licensed   \n2  Starbucks  47089-256771         Dana Mall       Licensed   \n3  Starbucks  22126-218024        Twofour 54       Licensed   \n4  Starbucks  17127-178586      Al Ain Tower       Licensed   \n\n                    Street Address              City State/Province Country  \\\n0                Av. Meritxell, 96  Andorra la Vella              7      AD   \n1             1 Street 69, Al Jarf             Ajman             AJ      AE   \n2     Sheikh Khalifa Bin Zayed St.             Ajman             AJ      AE   \n3                  Al Salam Street         Abu Dhabi             AZ      AE   \n4  Khaldiya Area, Abu Dhabi Island         Abu Dhabi             AZ      AE   \n\n  Postcode Phone Number                 Timezone  Longitude  Latitude  \n0    AD500    376818720  GMT+1:00 Europe/Andorra       1.53     42.51  \n1      NaN          NaN     GMT+04:00 Asia/Dubai      55.47     25.42  \n2      NaN          NaN     GMT+04:00 Asia/Dubai      55.47     25.39  \n3      NaN          NaN     GMT+04:00 Asia/Dubai      54.38     24.48  \n4      NaN          NaN     GMT+04:00 Asia/Dubai      54.54     24.51  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Brand</th>\n      <th>Store Number</th>\n      <th>Store Name</th>\n      <th>Ownership Type</th>\n      <th>Street Address</th>\n      <th>City</th>\n      <th>State/Province</th>\n      <th>Country</th>\n      <th>Postcode</th>\n      <th>Phone Number</th>\n      <th>Timezone</th>\n      <th>Longitude</th>\n      <th>Latitude</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Starbucks</td>\n      <td>47370-257954</td>\n      <td>Meritxell, 96</td>\n      <td>Licensed</td>\n      <td>Av. Meritxell, 96</td>\n      <td>Andorra la Vella</td>\n      <td>7</td>\n      <td>AD</td>\n      <td>AD500</td>\n      <td>376818720</td>\n      <td>GMT+1:00 Europe/Andorra</td>\n      <td>1.53</td>\n      <td>42.51</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Starbucks</td>\n      <td>22331-212325</td>\n      <td>Ajman Drive Thru</td>\n      <td>Licensed</td>\n      <td>1 Street 69, Al Jarf</td>\n      <td>Ajman</td>\n      <td>AJ</td>\n      <td>AE</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>GMT+04:00 Asia/Dubai</td>\n      <td>55.47</td>\n      <td>25.42</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Starbucks</td>\n      <td>47089-256771</td>\n      <td>Dana Mall</td>\n      <td>Licensed</td>\n      <td>Sheikh Khalifa Bin Zayed St.</td>\n      <td>Ajman</td>\n      <td>AJ</td>\n      <td>AE</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>GMT+04:00 Asia/Dubai</td>\n      <td>55.47</td>\n      <td>25.39</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Starbucks</td>\n      <td>22126-218024</td>\n      <td>Twofour 54</td>\n      <td>Licensed</td>\n      <td>Al Salam Street</td>\n      <td>Abu Dhabi</td>\n      <td>AZ</td>\n      <td>AE</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>GMT+04:00 Asia/Dubai</td>\n      <td>54.38</td>\n      <td>24.48</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Starbucks</td>\n      <td>17127-178586</td>\n      <td>Al Ain Tower</td>\n      <td>Licensed</td>\n      <td>Khaldiya Area, Abu Dhabi Island</td>\n      <td>Abu Dhabi</td>\n      <td>AZ</td>\n      <td>AE</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>GMT+04:00 Asia/Dubai</td>\n      <td>54.54</td>\n      <td>24.51</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('龙哥文件/starbucks_store_worldwide.csv')\n",
    "df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T07:31:53.178436800Z",
     "start_time": "2024-07-16T07:31:53.110529600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "    fruit   color  price\n0   apple     red    8.5\n1  banana  yellow    6.8\n2  orange  yellow    5.6\n3   apple    cyan    7.8\n4  banana    cyan    6.4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>fruit</th>\n      <th>color</th>\n      <th>price</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>apple</td>\n      <td>red</td>\n      <td>8.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>banana</td>\n      <td>yellow</td>\n      <td>6.8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>orange</td>\n      <td>yellow</td>\n      <td>5.6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>apple</td>\n      <td>cyan</td>\n      <td>7.8</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>banana</td>\n      <td>cyan</td>\n      <td>6.4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1=pd.DataFrame({'fruit':['apple','banana','orange','apple','banana'],'color':['red','yellow','yellow','cyan','cyan'],\n",
    "                  'price':[8.5,6.8,5.6,7.8,6.4]})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:12:32.262026400Z",
     "start_time": "2024-07-16T08:12:32.255560500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "g=df1.groupby(by='fruit')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:15:31.143155200Z",
     "start_time": "2024-07-16T08:15:31.131634500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple',    fruit color  price\n",
      "0  apple   red    8.5\n",
      "3  apple  cyan    7.8)\n",
      "('banana',     fruit   color  price\n",
      "1  banana  yellow    6.8\n",
      "4  banana    cyan    6.4)\n",
      "('orange',     fruit   color  price\n",
      "2  orange  yellow    5.6)\n"
     ]
    }
   ],
   "source": [
    "for i in g:\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:15:32.420965800Z",
     "start_time": "2024-07-16T08:15:32.412928400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "----------------------------------------\n",
      "   fruit color  price\n",
      "0  apple   red    8.5\n",
      "3  apple  cyan    7.8\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "banana\n",
      "----------------------------------------\n",
      "    fruit   color  price\n",
      "1  banana  yellow    6.8\n",
      "4  banana    cyan    6.4\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "orange\n",
      "----------------------------------------\n",
      "    fruit   color  price\n",
      "2  orange  yellow    5.6\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "for name,group in g:\n",
    "    print(name)  # 输出组名\n",
    "    print('-'*40)\n",
    "    print(group)  # 数据块\n",
    "    print(type(group))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:18:57.178995500Z",
     "start_time": "2024-07-16T08:18:57.175934400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "[('apple',\n     fruit color  price\n  0  apple   red    8.5\n  3  apple  cyan    7.8),\n ('banana',\n      fruit   color  price\n  1  banana  yellow    6.8\n  4  banana    cyan    6.4),\n ('orange',\n      fruit   color  price\n  2  orange  yellow    5.6)]"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取任意数据块\n",
    "list(df1.groupby(by='fruit'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:25:19.428347700Z",
     "start_time": "2024-07-16T08:25:19.405492900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 聚合"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "fruit   color \napple   cyan      7.8\n        red       8.5\nbanana  cyan      6.4\n        yellow    6.8\norange  yellow    5.6\nName: price, dtype: float64"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据水果来求价格的平均值\n",
    "df1.groupby(['fruit','color'])['price'].mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:53:10.398130200Z",
     "start_time": "2024-07-16T08:53:10.387046Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "fruit\napple     8.15\nbanana    6.60\norange    5.60\nName: price, dtype: float64"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 语法糖\n",
    "df1['price'].groupby(df1['fruit']).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:34:57.283960600Z",
     "start_time": "2024-07-16T08:34:57.277441400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "    fruit  price\n0   apple   8.15\n1  banana   6.60\n2  orange   5.60",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>fruit</th>\n      <th>price</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>apple</td>\n      <td>8.15</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>banana</td>\n      <td>6.60</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>orange</td>\n      <td>5.60</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# as_index,仅对dataframe生效，返回以组便签为索引对象\n",
    "df1.groupby('fruit',as_index=False)['price'].mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:45:41.905690600Z",
     "start_time": "2024-07-16T08:45:41.894643300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "fruit\napple     0.7\nbanana    0.4\norange    0.0\nName: price, dtype: float64"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算每一种水果的差值\n",
    "# 自定义聚合函数\n",
    "def diff(arr):\n",
    "    return arr.max()-arr.min()\n",
    "# agg aggregate\n",
    "df1.groupby('fruit')['price'].agg(diff)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T08:51:23.419500700Z",
     "start_time": "2024-07-16T08:51:23.413215500Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
